package apii.servlet.stud;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apii.server.ServerThings;
import apii.servlet.ServletUtils;
import apii.servlet.UserRequesterData;

/**
 * 
 * @author evgeniy
 */
public class ViewCodeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewCodeServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			UserRequesterData urd = ServletUtils.extractRequesterData(request
					.getSession());
			if (urd.sessionId == null || urd.role != UserRequesterData.ROLE_STUD
					|| urd.userId == UserRequesterData.UNKNOWN_ID
					|| !urd.logged) {
				request.getRequestDispatcher("/illegal").forward(request, response);
			} else {
				Connection c = ServerThings.getThings().getConnection();
				int codeId = (Integer) request.getAttribute(StudRootServlet.CODE_ID);
				CodeInfo ci = CodeInfo.getCodeInfo(c, codeId);
				c.close();
				
				PrintWriter pw = response.getWriter();
				ServletUtils.startCommonStudDoc(pw, urd, "Просмотр кода");
				pw.println("<center>" +
						ci.name +
						"</center>");
				// TODO: prepareCode is temp
				pw.println("<pre>" + prepareCode(ci.code) + "</pre>");
				ServletUtils.closeCommonStudDoc(pw, urd);
			}
		} catch (SQLException ex) {
			request.getRequestDispatcher("/error").forward(request, response);
			ex.printStackTrace();
		}
	}
	
	public static final String prepareCode(String code) {
		code = code.replaceAll("\t", "   ");
		code = code.replaceAll("public", "<b><b><span style=\"color:#8B008B\">public</span></b></b>");
		code = code.replaceAll("private", "<b><b><span style=\"color:#8B008B\">private</span></b></b>");
		code = code.replaceAll("try", "<b><b><span style=\"color:#8B008B\">try</span></b></b>");
		code = code.replaceAll("catch", "<b><b><span style=\"color:#8B008B\">catch</span></b></b>");
		code = code.replaceAll("if", "<b><b><span style=\"color:#8B008B\">if</span></b></b>");
		code = code.replaceAll("void", "<b><b><span style=\"color:#8B008B\">void</span></b></b>");
		code = code.replaceAll("int", "<b><b><span style=\"color:#8B008B\">int</span></b></b>");
		code = code.replaceAll("else", "<b><b><span style=\"color:#8B008B\">else</span></b></b>");
		code = code.replaceAll("return", "<b><b><span style=\"color:#8B008B\">return</span></b></b>");
		code = code.replaceAll("boolean", "<b><b><span style=\"color:#8B008B\">boolean</span></b></b>");
		code = code.replaceAll("true", "<b><b><span style=\"color:#8B008B\">true</span></b></b>");
		code = code.replaceAll("long", "<b><b><span style=\"color:#8B008B\">long</span></b></b>");
		code = code.replaceAll("double", "<b><b><span style=\"color:#8B008B\">double</span></b></b>");
		code = code.replaceAll("import", "<b><span style=\"color:#8B008B\">import</span></b>");
		
		return code;
	}
	
	private static class CodeInfo {

		public static final int UNCHECKED_STATE = 1;
		public static final int CHECKED_STATE = 2;
		public static final int ALERTED_STATE = 3;

		public final int id;
		public final int revision;
		public final String name;
		public final String code;
		public final int checkedState;

		public CodeInfo(int id, int revision, String name, String code, int checkedState) {
			this.id = id;
			this.revision = revision;
			this.name = name;
			this.checkedState = checkedState;
			this.code = code;
		}
		
		public static final CodeInfo getCodeInfo(Connection c, int codeId) throws SQLException {
			Statement s = c.createStatement();
			ResultSet codeRS = s.executeQuery(
					"SELECT apii.codes.revision, apii.codes.name, " +
					"apii.codes.code, apii.codes.checked " +
					"FROM apii.codes WHERE apii.codes.code_id = '" + codeId + "'");
			if (!codeRS.next()) {
				throw new Error();
			}
			CodeInfo ci = new CodeInfo(codeId, codeRS.getInt(1), codeRS.getString(2), codeRS.getString(3), 
					codeRS.getBoolean(4) ? CHECKED_STATE : UNCHECKED_STATE);
			return ci;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
}
